package jbase.project.system.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jbase.project.system.exception.SysException;

public class ContentLengthBlock {

	private int contentLength = -1;
	private ByteArrayOutputStream dataCache;
	private int readCnt;

	public ContentLengthBlock(int contentLength) {
		init(contentLength);
	}

	private void init(int contentLength) {
		this.contentLength = contentLength;
		this.dataCache = new ByteArrayOutputStream();
		this.readCnt = 0;
	}

	public int length() {
		return this.readCnt;
	}

	public void put(byte current) throws SysException {
		this.readCnt = this.readCnt + 1;
		try {
			dataCache.write(new byte[] { current });
		} catch (IOException e) {
			e.printStackTrace();
			throw new SysException(e);
		}
	}

	public boolean haveComplete() {
		return this.readCnt >= this.contentLength;
	}

	public ByteArrayOutputStream getByteArrayOutputStream() {
		return this.dataCache;
	}

	public String toString() {
		return new String(this.dataCache.toByteArray());
	}

}
